package action;

import java.util.Date;
import java.util.List;

import entity.News;
import service.NewsDAO;
import service.impl.NewsDAOImpl;

public class NewsAction extends SuperAction {
	
	public String add() {
		String title = request.getParameter("title");
		String content= request.getParameter("content");
		String userId = request.getParameter("userId");
		News news = new News();
		news.setTitle(title);
		news.setContent(content);
		news.setUserId(userId);
		news.setStatus(0);
		news.setCount(0);
		news.setTime(new Date());
		NewsDAO ndao = new NewsDAOImpl();
		ndao.addNews(news);
		return "addNews_success";
	}
	
	
	
	public String queryCheck() {
		NewsDAO ndao = new NewsDAOImpl();
		List<News> checkList = ndao.checkNews();
		session.setAttribute("news_checkAll", checkList);
//		for (int i = 0; i < newsAll.size(); i++) {
//			System.out.println(newsAll.get(i));
//		}
		return "queryCheck_success";
	}
	
	public String query() {
		NewsDAO ndao = new NewsDAOImpl();
		List<News> newsAll = ndao.queryAllNews();
		session.setAttribute("news_listAll", newsAll);
//		for (int i = 0; i < newsAll.size(); i++) {
//			System.out.println(newsAll.get(i));
//		}
		return "query_success";
	}
	public String queryById() {
		NewsDAO ndao = new NewsDAOImpl();
		String id = request.getParameter("nid");
		String type = request.getParameter("type");
		int nid = Integer.parseInt(id);
		News news = ndao.queryNewsById(nid);
		news.setCount(news.getCount() + 1);
		session.setAttribute("news", news);
		ndao.updateNews(news);
		if ("queryById".equals(type)) {
			return "queryById_success";
		}
		else {
			return "checkById_success";
		}
	}
	
	public String check() {
		String id = request.getParameter("nid");
		String status = request.getParameter("status");
		int nid = Integer.parseInt(id);
		NewsDAO ndao = new NewsDAOImpl();
		News news = ndao.queryNewsById(nid);
		if ("1".equals(status)) {
			news.setStatus(1);
		} 
		else {
			news.setStatus(2);
		}
		ndao.updateNews(news);
		return "check_success";
	}
}
